<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\LinesLog;
use App\Models\Lines;
use App\Models\Queue;
use App\Models\Order;
use App\Models\OrderGoods;
/**
 * 队列订单管理控制器
 */
// class LinesController extends Controller
class LinesController extends BaseController
{
    /**
     * 订单列表(GET) api/lines/list
     * 
     * @return json
     */
    public function list(Request $request)
    {
        // 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id
        // 测试数据
        // $shop_id = 1;

        if(!$shop_id){
            return self::error('未登录');
        }

        // 初始化数组
        $where = $return = array();

        $obj = new Lines();

        // 构造查询条件
        $obj = $obj->where('delivery_point',$shop_id);
        $obj = $obj->where('status','!=',0);

        // 检索条件
        $request->input('status')   == null || $obj = $obj->where('status',$request->input('status'));                      // 订单状态（1收益中 2兑换中 3已完成 4拒绝兑换）
        $request->input('tel')      == null || $obj = $obj->where('phone','like','%'.$request->input('tel').'%');           // 手机号
        $request->input('order_id') == null || $obj = $obj->where('order_id','like','%'.$request->input('order_id').'%');   // 订单号
        $request->input('stime')    == null || $obj = $obj->where('pay_time','>=',$request->input('stime'));                // 开始时间
        $request->input('etime')    == null || $obj = $obj->where('pay_time','<=',$request->input('etime'));                // 结束世家
        if($request->input('stime') && $request->input('etime')){
            $obj = $obj->whereBetween('pay_time',[$request->input('stime'),$request->input('etime')]);                      // 时间区间
        }

        // 查询列表
        $return = $obj->paginate(10);

        return self::json($return , '查询成功');
    }

    /**
     * 队列数据汇总(GET) api/lines/report
     */
    public function report(Request $request)
    {
        // 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id
        // 测试数据
        // $shop_id = 1;

        if(!$shop_id){
            return self::error('未登录');
        }

        $report = array();
        $report['total']    = $this->lcount($shop_id,'all');        // 总计
        $report['st_1']     = $this->lcount($shop_id,1);            // 收益中
        $report['st_2']     = $this->lcount($shop_id,2);            // 兑换中
        $report['st_3']     = $this->lcount($shop_id,3);            // 已兑换

        return $this->json($report);
    }

    /**
     * 统计
     */
    private function lcount($shop_id,$status='all')
    {   
        $obj = new Lines;
        switch ($status) {
            // 所有订单
            case 'all':
                $obj = $obj->where('status','!=',0);
                break;

            // 收益中订单
            case 1:
                $obj = $obj->where('status','=',1);
                break;

            // 兑换中
            case 2:
                $obj = $obj->where('status','=',2);
                break;

            // 已兑换
            case 3:
                $obj = $obj->where('status','=',3);
                break;

            default:
                break;
        }

        return $obj->where('delivery_point',$shop_id)->count();
    }

    /**
     * 用户提货(POST) api/lines/take
     * 
     * @return json
     */
    public function take(Request $request)
    {
        $order_id   = $request->input('order_id');        // 订单号
        $take_no    = $request->input('take_no');         // 提货码

        if(!$order_id || !$take_no){
            return self::error('参数不完整');
        }

        // 查询订单信息
        $order_info = Lines::where('order_id',$order_id)->where('take_delivery','1')->first();
        if(!$order_info){
            return self::error('订单不存在');
        }

        // 队列id
        $queue = Lines::where('order_id',$order_id)->value('queue');

        // 验证提货码
        if($order_info->take_no != $take_no){
            return self::error('提货码错误');
        }

        // 开启事务
        DB::beginTransaction();
        try{
        
            // 确认用户提货
            $re = Lines::where('order_id',$order_id)->update(['take_delivery'=>2]);
            if(!$re){
                return self::error('提货失败');
            }

            // 记录日志
            $lines_log = new LinesLog;
            $lines_log->queue       = $queue;
            $lines_log->order_id    = $order_id;
            $lines_log->type        = 3;            // 日志类型 3提货
            $lines_log->log         = "成功提货";
            $lines_log->save();

            // 提交事务
            DB::commit();

            return self::success('提货通过');

        }catch (\Exception $e) {

            // 回滚事务
            DB::rollback();

            return self::json($e , '审核出错' , 400);
        }

    }

    /**
     * 队列助推记录(GET) api/lines/dlzt
     */
    public function dlzt(Request $request)
    {
        $order_id   = $request->input('order_id');        // 订单号

        if(!$order_id){
            return self::error('参数不完整');
        }

        // 实例化模型
        $LinesLog   = new LinesLog;
        $Lines      = new Lines;
        $Queue      = new Queue;
        
        // 查询列表
        $data = DB::table($LinesLog['table'].' as ll')
                ->leftJoin($Lines['table'].' as l' , 'll.help_order' , '=' ,'l.order_id')
                ->leftJoin($Queue['table'].' as q' , 'l.queue' , '=' , 'q.id')
                ->where('ll.order_id',$order_id)
                ->where('ll.type',1)
                ->select('ll.help_order' , 'll.help_money' , 'll.created_at' , 'll.log' , 'l.money' , 'l.phone' , 'l.take_delivery' , 'l.status' , 'q.name')
                ->get();

        return self::json($data);
    }

    /**
     * 分润助推记录(GET) api/lines/frzt
     */
    public function frzt(Request $request)
    {
        $order_id   = $request->input('order_id');        // 订单号

        if(!$order_id){
            return self::error('参数不完整');
        }

        // 实例化模型
        $LinesLog   = new LinesLog;
        $Order      = new Order;
        $OrderGoods = new OrderGoods;

        // 查询列表
        $data = DB::table($LinesLog['table'].' as ll')
                ->leftJoin($Order['table'].' as o' , 'll.help_order' , '=' , 'o.order_sn')
                ->where('ll.order_id',$order_id)
                ->where('ll.type',2)
                ->select('ll.help_order' , 'll.help_money' , 'll.log' , 'o.created_at' , 'o.total_amount' , 'o.tel' , 'o.status')
                ->get();
        if($data){
            foreach ($data as $key => $value) {
                $data[$key]->goods = OrderGoods::where('order_sn',$value->help_order)->value('goods_name');
            }
        }


        return self::json($data);
    }

    

    

}
